<?php
include_once '../util/db/Conn.php';
include_once '../util/ArrayList.php';
include_once '../util/db/model/PreorderList.php';
include_once '../util/db/model/PreorderDetail.php';
include_once '../util/db/model/Product.php';
include_once '../util/db/model/ProdAttr.php';
include_once '../util/db/model/ProdType.php';
include_once '../util/db/model/ProdSubType.php';
include_once '../util/db/model/SupplierBrand.php';
include_once '../util/db/model/OrderListSearchPara.php';

?>
<?php

/**
 * 
 * 預購訂單
 * @author Wade
 *
 */
class PreorderListDBUtil {
	protected $dbConn;
	
	private $tabName = "preorder_list";
	
	private $preorderListSQL;
	
	private $preorderDetailSQL;
	
	function __construct() {
		$this->dbConn = new DBConn ();
		@header ( "Content-Type:text/html; charset=utf-8" );
		
		$this->preorderListSQL = " select * ";
		$this->preorderListSQL = $this->preorderListSQL . ", sb.pic sb_pic ";
		$this->preorderListSQL = $this->preorderListSQL . " from $this->tabName pl ";
		$this->preorderListSQL = $this->preorderListSQL . ", supplier_brand sb ";
		$this->preorderListSQL = $this->preorderListSQL . " where 0=0 ";
		$this->preorderListSQL = $this->preorderListSQL . " and pl.sb_id=sb.sb_id ";
		
		$this->preorderDetailSQL = " select * ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", pd.status pd_status ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", pd.count pd_count ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", pd.price pd_price ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", pd.count pd_count ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", pa.count pa_count ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", sb.pic sb_pic ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", p.price p_price ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " from $this->tabName pl ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", preorder_detail pd ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", product p ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", prod_attr pa ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . ", supplier_brand sb ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " where 0=0 ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " and pl.sb_id=sb.sb_id ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " and pd.pl_id=pl.pl_id ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " and pd.pa_id=pa.pa_id ";
		$this->preorderDetailSQL = $this->preorderDetailSQL . " and pa.p_id=p.p_id ";
	}
	
	public function open() {
		$this->dbConn->open ();
	}
	
	public function close() {
		$this->dbConn->close ();
	}
	
	public function queryByPd_id($pd_id) {
		$this->open ();
		$sql = " select * ";
		$sql = $sql . ", pd.status pd_status ";
		$sql = $sql . ", pd.price pd_price ";
		$sql = $sql . ", pd.count pd_count ";
		$sql = $sql . " from preorder_detail pd where pd.pd_id=$pd_id";
		$result = mysql_query ( $sql );
		$preorderDetail = null;
		while ( $row = @mysql_fetch_assoc ( $result ) ) {
			$preorderDetail = $this->genPreorderDetail ( $row );
		}
		$this->close ();
		return $preorderDetail;
	}
	
	private function genPreorderDetail($row) {
		$preorderDetail = new PreorderDeail ();
		$preorderDetail->setPd_id ( $row ['pd_id'] );
		$preorderDetail->setPl_id ( $row ['pl_id'] );
		$preorderDetail->setPa_id ( $row ['pa_id'] );
		$preorderDetail->setPrice ( $row ['pd_price'] );
		$preorderDetail->setCount ( $row ['pd_count'] );
		$preorderDetail->setStatus ( $row ['pd_status'] );
		return $preorderDetail;
	}
	
	private function fillPreorderDetail($row) {
		//		$preorderDetail = new PreorderDeail ();
		//		$preorderDetail->setPd_id ( $row ['pd_id'] );
		//		$preorderDetail->setPl_id ( $row ['pl_id'] );
		//		$preorderDetail->setPa_id ( $row ['pa_id'] );
		//		$preorderDetail->setPrice ( $row ['pd_price'] );
		//		$preorderDetail->setCount ( $row ['pd_count'] );
		$preorderDetail = $this->genPreorderDetail ( $row );
		
		$preorderDetail->setPreorderList ( $this->fillPreorderList ( $row ) );
		
		$product = new Product ();
		$product->setP_id ( $row ['p_id'] );
		$product->setProd_no ( $row ['prod_no'] );
		$product->setP_name ( $row ['p_name'] );
		$product->setAdd_date ( $row ['add_date'] );
		$product->setSort ( $row ['sort'] );
		$product->setSb_id ( $row ['sb_id'] );
		$product->setPrice ( $row ['p_price'] );
		$product->setSell_price ( $row ['sell_price'] );
		$product->setSell_mode ( $row ['sell_mode'] );
		$product->setPre_year ( $row ['pre_year'] );
		$product->setPre_month ( $row ['pre_month'] );
		$product->setPre_day ( $row ['pre_day'] );
		$product->setShip_month ( $row ['ship_month'] );
		$product->setShip_day ( $row ['ship_day'] );
		$product->setTitle ( $row ['title'] );
		$product->setPic ( $row ['pic'] );
		$product->setProd_txt ( $row ['prod_txt'] );
		$preorderDetail->setProduct ( $product );
		
		$prodAttr = new ProdAttr ();
		$prodAttr->setAttr ( $row ['attr'] );
		$prodAttr->setPa_id ( $row ['pa_id'] );
		$prodAttr->setCount ( $row ['pa_count'] );
		$prodAttr->setProduct ( $product );
		$preorderDetail->setProdAttr ( $prodAttr );
		
		return $preorderDetail;
	}
	/**
	 * Enter description here ...
	 */
	private function fillPreorderList($row) {
		$preorderList = new PreorderList ();
		$preorderList->setPl_id ( $row ["pl_id"] );
		$preorderList->setPreorder_no ( $row ["preorder_no"] );
		$preorderList->setPreorder_date ( $row ["preorder_date"] );
		$preorderList->setSb_id ( $row ["sb_id"] );
		
		$supplierBrand = new SupplierBrand ();
		$supplierBrand->setSb_id ( $row ['sb_id'] );
		$supplierBrand->setCm_id ( $row ['cm_id'] );
		$supplierBrand->setLogo_name ( $row ['logo_name'] );
		$supplierBrand->setLogo_name_en ( $row ['logo_name_en'] );
		$supplierBrand->setPic ( $row ['sb_pic'] );
		$supplierBrand->setAddr ( $row ['addr'] );
		$supplierBrand->setTel ( $row ['tel'] );
		$preorderList->setSupplierBrand ( $supplierBrand );
		return $preorderList;
	}
	
	//	public function queryAllByPara($para) {
	//		$this->open ();
	//		
	//		$arrayList = new ArrayList ();
	//		$sql = $this->joinOrderListSql . " where 1=1 ";
	//		//		echo "para->getP_name ()=".$para->getP_name ()."<br />";
	//		//		if (null != $para->getP_name () && $para->getP_name () != "") {
	//		//			$sql = $sql . " and a.p_name like '%" . $para->getP_name () . "%' ";
	//		//		}
	//		//		echo "para->getOrderDate ()=".$para->getOrderDate ()."<br />";
	//		if (null != $para->getOrderDate () && $para->getOrderDate () != "") {
	//			$sql = $sql . " and a.add_date=DATE_FORMAT('" . $para->getOrderDate () . "', '%Y%m%d') ";
	//		}
	//		//		echo "para->getSb_id ()=".$para->getSb_id ()."<br />";
	//		if (null != $para->getSb_id () && $para->getSb_id () != "") {
	//			$sql = $sql . " and a.sb_id =" . $para->getSb_id () . " ";
	//		}
	//		
	//		//		echo $sql;
	//		$result = mysql_query ( $sql );
	//		while ( $row = @mysql_fetch_array ( $result ) ) {
	//			$arrayList->add ( $this->fillOrderList ( $row ) );
	//		}
	//		$this->close ();
	//		return $arrayList;
	//	
	//	}
	

	//	public function setOrderListCheck($pl_id) {
	//		$this->open ();
	//		$sql = "update `$this->tabName` set status=1 where ol_id=$pl_id";
	//		mysql_query ( $sql );
	//		$this->close ();
	//	}
	

	public function acceptPreorderDetail($pd_id) {
		$preorderDetail = $this->queryByPd_id ( $pd_id );
		if ($preorderDetail->getStatus () == 1) { //若為已出貨
			$this->open ();
			//將出貨狀態改為已點收
			$sql = " update preorder_detail set ";
			$sql = $sql . " status=2 ";
			$sql = $sql . ", accept_date=NOW() ";
			$sql = $sql . " where status=1 ";
			$sql = $sql . " and pd_id=$pd_id";
			mysql_query ( $sql );
			//			echo $sql . "<br>";
			

			$sql = " update prod_attr set ";
			$sql = $sql . " count=count+" . $preorderDetail->getCount ();
			$sql = $sql . " where pa_id=" . $preorderDetail->getPa_id ();
			mysql_query ( $sql );
			//			echo $sql;
			$this->close ();
		}
	}
	
	public function queryPreorderDetail($orderListSearchPara) {
		$sql = $this->preorderDetailSQL;
		//		if (! is_null ( $orderListSearchPara->getStatus () )) {
		//			$sql = $sql . " and pd.status= " . $orderListSearchPara->getStatus (); //訂單狀態
		//		}
		if (! is_null ( $orderListSearchPara->getP_id () )) {
			$sql = $sql . " and p.p_id=" . $orderListSearchPara->getP_id ();
		}
		if (! is_null ( $orderListSearchPara->getSb_id () )) {
			$sql = $sql . " and pl.sb_id=" . $orderListSearchPara->getSb_id ();
		}
		if (! is_null ( $orderListSearchPara->getP_name () )) {
			$sql = $sql . " and p.p_name like '%" . $orderListSearchPara->getP_name () . "%' ";
		}
		if (! is_null ( $orderListSearchPara->getPd_id () )) {
			$sql = $sql . " and pd.pd_id=" . $orderListSearchPara->getPd_id ();
		}
		//訂單狀態
		if (! is_null ( $orderListSearchPara->getStatus () )) {
			if ($orderListSearchPara->getStatus () == "A") { //若為查詢進貨資料
				$sql = $sql . " and pd.status in (0, 1)";
			} else if ($orderListSearchPara->getStatus () == "B") { //若為查詢退貨資料
				$sql = $sql . " and pd.status in (2, 3)";
			} else {
				$sql = $sql . " and pd.status=" . $orderListSearchPara->getStatus ();
			}
		}
		
		if (! is_null ( $orderListSearchPara->getYear () )) {
			$sql = $sql . " and year(preorder_date)=" . $orderListSearchPara->getYear ();
		}
		if (! is_null ( $orderListSearchPara->getMonth () )) {
			$sql = $sql . " and month(preorder_date)=" . $orderListSearchPara->getMonth ();
		}
//echo "$sql<br>";
		$this->open ();
		$result = mysql_query ( $sql );
		$preorderDetailArray = new ArrayList ();
		while ( $row = @mysql_fetch_assoc ( $result ) ) {
			$preorderDetailArray->add ( $this->fillPreorderDetail ( $row ) );
		}
		$this->close ();
		return $preorderDetailArray;
	}
	
	public function genStatusDesc($status){
		$statusDesc = "";
		switch ($status) {
			case 1:
				$statusDesc="未點收";
				break;
			case 2:
				$statusDesc="點收";
				break;
		}
		return $statusDesc;
	}
}
?>